package com.nuanshui.heatedloan.credit.service;

import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.heatedloan.baseservice.service.CommonServiceImpl;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.credit.constant.BorrowType;
import com.nuanshui.heatedloan.credit.constant.HomePageConstant;
import com.nuanshui.heatedloan.credit.constant.UserStatusType;
import com.nuanshui.heatedloan.credit.facade.*;
import com.nuanshui.heatedloan.credit.form.CustomerHomePageInfoForm;
import com.nuanshui.heatedloan.credit.model.LightLoanProductInfo;
import com.nuanshui.heatedloan.credit.model.ProductInfo;
import com.nuanshui.heatedloan.credit.model.SingleLoanProductInfo;
import com.nuanshui.heatedloan.credit.vo.CustomerBlackListVO;
import com.nuanshui.heatedloan.credit.vo.CustomerCreditResultVO;
import com.nuanshui.heatedloan.credit.vo.CustomerHomePageInfoVO;
import com.nuanshui.heatedloan.credit.vo.PayOrderInfoVO;
import com.nuanshui.heatedloan.userinfo.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zss
 * @date 2017/7/10 19:09
 * @description
 * @Since
 */
@Transactional(readOnly=true,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
@Service(ServiceConstant.CUSTOMERHOMEPAGEINFOSERVICE)
public class CustomerHomePageInfoServiceImpl extends CommonServiceImpl implements CustomerHomePageInfoService {

    private Logger logger =LoggerFactory.getLogger(CustomerHomePageInfoForm.class);

    @Autowired
    private CustomerCreditResultService customerCreditResultService;

    @Autowired
    private CustomerProduceService customerProduceService;

    @Autowired
    private CustomerBlackListService customerBlackListService;

    @Autowired
    private  PayOrderInfoService payOrderInfoService;

    @Override
    public Integer getCustomerStatus(String userId){

        Integer status=null;


        while (true)
        {
            CustomerBlackListVO customerBlackListVO=customerBlackListService.loadVO(userId);
            if(customerBlackListVO!=null){
                //冻结状态
                status=UserStatusType.BLOCKED_ACCOUNT;
                break;
            }
            //
            CustomerCreditResultVO customerCreditResultVO=customerCreditResultService.loadVOByUserId(userId);
            if(customerCreditResultVO==null){
                //未计算额度
                status=UserStatusType.NOT_COMPUTE_AMOUNT;
                break;
            }
            //计算额度中
            if(customerCreditResultVO!=null&&customerCreditResultVO.getStatus()==1){
                status=UserStatusType.COMPUTING_AMOUNT;
                break;
            }

            List<PayOrderInfoVO>  payOrderInfoVO = payOrderInfoService.loadValidPayOrderInfoVOByUserId(userId);
            if(payOrderInfoVO.size()>0){ //有待还款账单(逾期，未逾期)
                status=UserStatusType.CERTIFIED_LOAD;
            }else{//已认证，无待还款记录
                status=UserStatusType.CERTIFIED;
            }
            break;
        }

        return status;
    }

    private CustomerHomePageInfoVO convertFromProductInfo(ProductInfo item)
    {
        CustomerHomePageInfoVO info=new CustomerHomePageInfoVO();
        info.setName(item.getName());
        info.setContent(item.getContent());
        info.setAmountContent(info.getContent());
        info.setType(item.getType());

        return info;
    }

    private Integer getValidSingleLoanAmt(CustomerCreditResultVO customerCreditResultVO,SingleLoanProductInfo singleLoanProductInfo)
    {
        Integer sum = payOrderInfoService.loadSumByUserId(getCurrentUserId());

        Integer validAmt=customerCreditResultVO.getSingleAmt();

        if (sum != null && sum >= 0) {//有贷款状态

            validAmt = customerCreditResultVO.getSingleAmt()-sum;
        }

        return validAmt;
    }

    @Override
    public CustomerHomePageInfoVO loadVOByType(Integer type) {

        CustomerHomePageInfoVO item=null;
        if(type==BorrowType.SINGLELOAN)
        {
            SingleLoanProductInfo singleLoanProductInfo=this.customerProduceService.getSingleLoanProductInfo();
            item=convertFromProductInfo(singleLoanProductInfo);
        }
        else if(type==BorrowType.LIGHTLOAN)
        {
            LightLoanProductInfo lightLoanProductInfo=this.customerProduceService.getLightLoanProductInfo();
            item=convertFromProductInfo(lightLoanProductInfo);
            //item.setAmountContent(this.getLightLoanDefault(lightLoanProductInfo));
        }
        return item;
    }

    @Override
    public List<CustomerHomePageInfoVO> getDefaultList()
    {
        List<CustomerHomePageInfoVO> productList=new ArrayList<CustomerHomePageInfoVO>();

        productList.add(loadVOByType(BorrowType.SINGLELOAN));
        productList.add(loadVOByType(BorrowType.LIGHTLOAN));

        return productList;
    }

    /**
     * 获取闪电贷默认模板信息
     *
     * @return
     */
    private String getLightLoanDefault(LightLoanProductInfo lightLoanProductInfo) {
        String[] amtList = lightLoanProductInfo.getProductLimit().split(",");
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < amtList.length; i++) {
            if (i == (amtList.length - 1)) {//最后数字没有逗号
                sb.append(Integer.parseInt(amtList[i]) / 100);
            } else {
                sb.append(Integer.parseInt(amtList[i]) / 100).append(",");
            }
        }
        sb.append(HomePageConstant.LIGHTLOAN_AUTH_DEFAULT_TEXT);
        return sb.toString();
    }
}
